package com.industrial.operation.controller;

import cn.hutool.core.util.ArrayUtil;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.operation.domain.OperationCityCheckNew;
import com.industrial.operation.service.IOperationCityCheckNewService;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 十六市地市考核Controller
 *
 * @author ruoyi
 * @date 2024-04-08
 */
@RestController
@RequestMapping("/sl/city/kh")
public class OperationCityCheckNewController extends BaseController {

    @Autowired
    private IOperationCityCheckNewService operationCityCheckService;

    /**
     * 查询十六市地市考核列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OperationCityCheckNew operationCityCheckNew)
    {
        startPage();

        Set<String> roles = SecurityUtils.getLoginUser().getRoles();
        if (ArrayUtil.contains(roles.toArray(), "s_01_011") || ArrayUtil.contains(roles.toArray(),"Sixteen_Zones")) {
            operationCityCheckNew.setCityCode(SecurityUtils.getLoginUser().getSysUser().getCity());
        }
        List<OperationCityCheckNew> list = operationCityCheckService.selectOperationCityCheckList(operationCityCheckNew);
        return getDataTable(list);
    }

    /**
     * 导出十六市地市考核列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, OperationCityCheckNew operationCityCheckNew)
    {
        List<OperationCityCheckNew> list = operationCityCheckService.selectOperationCityCheckList(operationCityCheckNew);
        ExcelUtil<OperationCityCheckNew> util = new ExcelUtil<OperationCityCheckNew>(OperationCityCheckNew.class);
        util.exportExcel(response, list, "十六市地市考核数据");
    }

    /**
     * 获取十六市地市考核详细信息
     */
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(operationCityCheckService.selectOperationCityCheckByBaseId(baseId));
    }

    /**
     * 新增十六市地市考核
     */
    @PostMapping
    public AjaxResult add(@RequestBody OperationCityCheckNew operationCityCheckNew)
    {
        return toAjax(operationCityCheckService.insertOperationCityCheck(operationCityCheckNew));
    }

    /**
     * 修改十六市地市考核
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OperationCityCheckNew operationCityCheckNew)
    {
        return toAjax(operationCityCheckService.updateOperationCityCheck(operationCityCheckNew));
    }

    /**
     * 删除十六市地市考核
     */
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(operationCityCheckService.deleteOperationCityCheckByBaseIds(baseIds));
    }

    /**
     * <p>
     *     导入数据模版文件
     * </p>
     * @param file 文件
     * @param years 年份
     * @return
     */
    @PostMapping("/importXlsxTemplate")
    public AjaxResult importXlsxTemplate(@RequestPart("file") MultipartFile file, String years){
        if(StringUtils.isBlank(years)){
            return AjaxResult.warn("请选择导入的年份");
        }
        operationCityCheckService.importXlsxTemplate(file,years);
        return AjaxResult.success();
    }

    /**
     * 查询统计的柱状图
     * @return
     */
    @GetMapping("/queryTongJiEcharts")
    public AjaxResult queryTongJiEcharts(OperationCityCheckNew operationCityCheckNew){
        Map<String,Object> result = operationCityCheckService.queryTongJiEcharts(operationCityCheckNew);
        return AjaxResult.success(result);
    }

    /**
     * 查询统计表格中的数据
     * @return
     */
    @GetMapping("/queryTongJiTable")
    public AjaxResult queryTongJiTable(OperationCityCheckNew operationCityCheckNew){
        List<OperationCityCheckNew> result = operationCityCheckService.queryTongJiTable(operationCityCheckNew);
        return AjaxResult.success(result);
    }
}
